package com.lczyfz.istep.common.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by DH on 2017/7/5.
 */
public class CheckUtils {
    public static void main(String[] args) {
        System.out.println(isIpInRange("10.10.20.10","10.10.20.0/24"));
//        System.out.println("10.0.0.0/24".matches("((?:(?:[01]\\d?\\d|2(?:[0-4]\\d|5[0-5]))\\.){3}(?:[01]\\d?\\d|2(?:[0-4]\\d|5[0-5])))\\/\\d(\\.\\d)?"));
        System.out.println(isIp("10.0.0.0/24"));
        System.out.println(isContainChinese("10.0发2.0.0.12"));

        try{
            isIpInRange("192.168.0.ff1","192.168.0s.0/24");
        }catch (Exception e)
        {
            System.out.println("ffff");

        }
    }


    public static boolean isIpInRange(String ip, String cidr) {
        String[] ips = ip.split("\\.");
        int ipAddr = (Integer.parseInt(ips[0]) << 24)
                | (Integer.parseInt(ips[1]) << 16)
                | (Integer.parseInt(ips[2]) << 8)
                | Integer.parseInt(ips[3]);
        int type = Integer.parseInt(cidr.replaceAll(".*/", ""));
        int mask = 0xFFFFFFFF << (32 - type);
        String cidrIp = cidr.replaceAll("/.*", "");
        String[] cidrIps = cidrIp.split("\\.");
        int cidrIpAddr = (Integer.parseInt(cidrIps[0]) << 24)
                | (Integer.parseInt(cidrIps[1]) << 16)
                | (Integer.parseInt(cidrIps[2]) << 8)
                | Integer.parseInt(cidrIps[3]);

        return (ipAddr & mask) == (cidrIpAddr & mask);
    }

    public static boolean isIp(String ip) {
        String str = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
                + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
                + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
                + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
        Pattern pattern = Pattern.compile(str);
        Matcher matcher = pattern.matcher(ip);
        return matcher.matches();
    }
    public static boolean isContainChinese(String str) {
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(str);
        return m.find();
    }
}

